package cc.comac.mom.datahandlers.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipDocumentManagement;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipDocumentManagementDao;
import cc.comac.mom.entity.EquipDocumentManagementEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.GetGeneralFieldValue;

@Component
@Transactional
public class EquipDocumentManagementDataHandler
		extends ServiceImpl<EquipDocumentManagementDao, EquipDocumentManagementEntity> {

	@Autowired
	private EquipDocumentManagementDao equipDocumentManagementDao;
	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;

	public int insert(EquipDocumentManagement equipDocumentManagement) {
		if (StringUtils.isEmpty(equipDocumentManagement.getDocumentName())) {
			throw new RuntimeException("无法获取上传的文件名！");
		}
		// 查询文档名称是否重复
		QueryWrapper<EquipDocumentManagementEntity> queryWrapper = new QueryWrapper<EquipDocumentManagementEntity>();
		queryWrapper.eq("document_name", equipDocumentManagement.getDocumentName());
		queryWrapper.eq("row_status", 1);
		EquipDocumentManagementEntity selectOne = equipDocumentManagementDao.selectOne(queryWrapper);
		if (selectOne != null) {
			throw new RuntimeException("当前文件名" + equipDocumentManagement.getDocumentName() + "已存在，无法新增！");
		}
		baseEntityDataUtil.updateBaseEntityData(equipDocumentManagement);
		int insert = equipDocumentManagementDao.insert(equipDocumentManagement);
		return insert;
	}

	public boolean batchDelete(List<EquipDocumentManagement> list) {
		// 预留删除检验
		// ..........

		List<EquipDocumentManagementEntity> list2 = new ArrayList<EquipDocumentManagementEntity>();
		list.forEach(item -> {
			EquipDocumentManagementEntity equipDocumentManagementEntity = new EquipDocumentManagementEntity();
			equipDocumentManagementEntity.setId(item.getId());
			equipDocumentManagementEntity.setRowStatus(0);
			baseEntityDataUtil.updateBaseEntityData(equipDocumentManagementEntity);
			list2.add(equipDocumentManagementEntity);
		});
		boolean updateBatchById = updateBatchById(list2, list2.size());
		return updateBatchById;
	}

	public ResultDev selectAllByPage(EquipDocumentManagement equipDocumentManagement) {
		// 预留校验
		// .........
		if (equipDocumentManagement.getRelationId() == 0) {
			throw new RuntimeException("请选择一条基础数据！");
		}
		ResultDev resultDev = new ResultDev();
		QueryWrapper<EquipDocumentManagementEntity> queryWrapper = new QueryWrapper<EquipDocumentManagementEntity>();
		queryWrapper.eq("row_status", 1).eq("relation_id", equipDocumentManagement.getRelationId()).eq("document_soure",
				equipDocumentManagement.getDocumentSoure());
		queryWrapper.orderByDesc("creation_time");
		IPage<EquipDocumentManagementEntity> page = new Page<EquipDocumentManagementEntity>(
				equipDocumentManagement.getPageNum(), equipDocumentManagement.getPageSize(), true);

		IPage<EquipDocumentManagementEntity> ipage = equipDocumentManagementDao.selectPage(page, queryWrapper);
		resultDev.setEntityList(ipage.getRecords());
		resultDev.setPageCount((int)ipage.getPages());
		resultDev.setTotalQuantity((int)ipage.getTotal());
		return resultDev;
	}

	public EquipDocumentManagementEntity getById(Serializable id) {
		return equipDocumentManagementDao.selectById(id);
	}

	@Override
	public boolean saveBatch(Collection<EquipDocumentManagementEntity> entityList, int batchSize) {
		if (entityList.size() == 0) {
			throw new RuntimeException("传入的文档管理集合为空！");
		}
		List<String> list = new ArrayList<>();
		List<Long> listId = new ArrayList<>();
		String documentSoure = "";
		for (EquipDocumentManagementEntity equipDocumentManagementEntity : entityList) {
			documentSoure = equipDocumentManagementEntity.getDocumentSoure();
			baseEntityDataUtil.setBaseEntityData(equipDocumentManagementEntity);
			list.add(equipDocumentManagementEntity.getDocumentName());
			listId.add(equipDocumentManagementEntity.getRelationId());
		}
		QueryWrapper<EquipDocumentManagementEntity> queryWrapper = new QueryWrapper<EquipDocumentManagementEntity>();
		queryWrapper.in("document_name", list);
		queryWrapper.in("relation_id",listId);
		queryWrapper.eq("row_status", 1).eq("document_soure", documentSoure);
		List<EquipDocumentManagementEntity> selectList = equipDocumentManagementDao.selectList(queryWrapper);
		if (selectList.size() > 0) {
			String fileName = "";
			for (EquipDocumentManagementEntity equipDocumentManagementEntity : selectList) {
				fileName = fileName + "/" + equipDocumentManagementEntity.getDocumentName();
			}
			throw new RuntimeException("文件名" + fileName + "已存在！");
		}
		return super.saveBatch(entityList, batchSize);
	}

}
